/**
 * Super Class User provides resources common to all users including
 * holding authentication information and determining permissions for
 * functionality based on user type.
 */
package docoffice.doss;
import java.util.logging.Logger;
import java.sql.ResultSet;

import docoffice.database.DatabaseManager;


/**
 * @author Ray Nosari 
 * @version $Revision: 1.0 $
 */
public class User{
	
	/**
	 * Fields username, password, userType
	 */
	private String username, password, userType;
	
	/**
	 * Field LOG.
	 */
	private static final Logger LOG = Logger.getLogger("User");
	
	/**
	 * Field isLocked;
	 */
	private boolean isLocked;
		
	/**
	 * Constructor for User
	 * @param username
	 * @param password
	 * @param userType
	 */
	public User(String username, String password, String userType){
		this.username = username;
		this.password = password;
		this.userType = userType;
	}
	
	/**
	 * Constructor overload for user
	 */
	public User(){
		this.username = "";
		this.password = "";
		this.userType = "";
	}
	
	/**
	 * username getter
	 * @return username
	 */
	public String getUsername(){
		return username;
	}
	
	/**
	 * username setter
	 * @param username
	 */
	public void setUsername(String username){
		this.username = username;
	}
	
	/**
	 * password getter
	 * @return password
	 */
	public String getPassword(){
		return password;
	}
	
	/**
	 * password setter
	 * @param password
	 */
	public void setPassword(String password){
		this.password = password;
	}
	
	/**
	 * userType getter
	 * @return userType
	 */
	public String getUserType(){
		return userType;
	}
	
	/**
	 * userType setter
	 * @param userType
	 */
	public void setUserType(String userType){
		this.userType = userType;
	}
	
	/**
	 * isLocked getter
	 * @return isLocked
	 */
	public boolean getIsLocked(){
		return this.isLocked;
	}
	
	/**
	 * isLocked setter
	 * @param isLocked
	 */
	public void setIsLocked(boolean isLocked){
		this.isLocked = isLocked;
	}
	
	/**
	 * toString overloaded method
	 * @return ret
	 */
	public String toString(){
		final String ret = "This class contains User information";
		return ret; 
	}
	
	//CRUD Methods
	/**
	 * creates a new user and adds it to the database, will be overloaded by subclasses
	 * @param username
	 * @param password
	 */
	public void createUser(){
		
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "INSERT INTO login (Username, Password, isLocked, UserType)" +
				" VALUES ('" + this.username + "', '" + this.password + "', 0, '" + this.userType + "')";
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error has occured while creating a new user.");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("A new user has been created");
	}
	
	/**
	 * gets user information based on username, will be overloaded by subclasses
	 * @param username
	 * @return user
	 */
	public User readUser(){
		final User user = new User();
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "SELECT * FROM login WHERE Username = " + this.username;
		ResultSet rs = null;
		try{
			rs = dbMgr.runReadQuery(sqlQuery);
			final boolean hasItem = rs.first();
			if (hasItem){
				final String password = rs.getString("Password");
				final boolean isLocked = rs.getBoolean("isLocked");
				final String userType = rs.getString("UserType");
				user.setPassword(password);
				user.setIsLocked(isLocked);
				user.setUserType(userType);
			}
		} catch(Exception e){
			LOG.severe("A SQL Error ocurred while reading user info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection(rs);
		}
		
		LOG.info("User Info was read from the database");
		return user;
	}
	
	/**
	 * updates user information in the database, will be overloaded by subclasses
	 * @param user
	 */
	public void updateUser(){
		final String username = this.username;
		final String password = this.password;
		final boolean isLocked = this.isLocked;
		final String userType = this.userType;
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "UPDATE login SET Password = " + 
		      password + ", isLocked = " + isLocked + ", UserType = " + userType + "WHERE Username = " + username;
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error occured while updating user info");
			e.printStackTrace();
		} finally{
			dbMgr.closeConnection();
		}
		
		LOG.info("User info was sucessfully updated");
	}
	
	/**
	 * deletes the user in the database based on username, 
	 * will be overloaded by subclasses
	 * @param user
	 */
	public void deleteUser(){
		final String username = this.username;
		final DatabaseManager dbMgr = new DatabaseManager();
		final String sqlQuery = "DELETE FROM login WHERE Username = " + username;
		try{
			dbMgr.runUpdateQuery(sqlQuery);
		} catch(Exception e){
			LOG.severe("A SQL Error ocurred while deleting user");
			e.printStackTrace();
		} finally {
			dbMgr.closeConnection();
		}
	}
	
}